package com.itc.dispatch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.utils.OkHttpUtil;
import com.itc.common.core.utils.StringUtils;
import com.itc.dispatch.domain.FlightOperationMonitoringPtd;
import com.itc.dispatch.domain.FlightTimeExchange;
import com.itc.dispatch.service.FlightTimeExchangeService;
import com.itc.dispatch.mapper.FlightTimeExchangeMapper;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 91516
* @description 针对表【flight_time_exchange(航班时隙交换信息表)】的数据库操作Service实现
* @createDate 2024-09-03 13:12:59
*/
@Service
public class FlightTimeExchangeServiceImpl extends ServiceImpl<FlightTimeExchangeMapper, FlightTimeExchange>
    implements FlightTimeExchangeService{

    @Override
    public void timeslotExchange() {
        //api获取时隙交换数据
        Object resultObj = OkHttpUtil.sendOkHttpClient("http://172.23.80.2:8080/alg/timeslotExchange"
                , "GET",null,null);
        if(resultObj == null){
            return;
        }
        String dataString = resultObj.toString();
        JSONObject dataJson = JSONObject.parseObject(dataString);
        if(!StringUtils.equals("200",dataJson.getString("code"))){
            System.out.println(dataJson);
            return;
        }

        JSONArray timeArray = JSONArray.parseArray(dataJson.getString("data"));
        try{
            for (Object o : timeArray) {
                FlightTimeExchange timeExchange = JSONObject.parseObject(o.toString(),FlightTimeExchange.class);

                this.getBaseMapper().delete(
                        new LambdaQueryWrapper<FlightTimeExchange>()
                                .eq(FlightTimeExchange::getFlt1Id,timeExchange.getFlt1Id())
                                .eq(FlightTimeExchange::getFlt2Id,timeExchange.getFlt2Id())
                );
                this.save(timeExchange);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }
}




